<template>
  <el-dialog title="编辑用户" :close-on-click-modal="false" @close="close" :visible.sync="visible">
    <el-form :model="form" :rules="rules" ref="form" v-loading="loading">
      <el-form-item label="头像" :label-width="formLabelWidth" prop="icon">
        <c-avatar-upload v-model="form.icon" size="small"></c-avatar-upload>
      </el-form-item>
      <el-row>
        <el-col :span="12">
          <el-form-item label="用户名" :label-width="formLabelWidth" prop="userName">
            <el-input v-model="form.userName"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="姓名" :label-width="formLabelWidth" prop="realName">
            <el-input v-model="form.realName"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="手机" :label-width="formLabelWidth" prop="mobile">
            <el-input v-model="form.mobile"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="生日" :label-width="formLabelWidth" prop="birthday">
            <el-date-picker
              v-model="form.birthday"
              @change="v => form.birthday=v"
              type="date"
              placeholder="出生日期">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="email" :label-width="formLabelWidth" prop="email">
            <el-input v-model="form.email"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="QQ" :label-width="formLabelWidth" prop="qq">
            <el-input v-model="form.qq"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="微信" :label-width="formLabelWidth" prop="wx">
            <el-input v-model="form.wx"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="部门" :label-width="formLabelWidth" prop="departIds">
            <c-select v-model="form.departIds" type="Department" placeholder="请选择"></c-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="角色" :label-width="formLabelWidth" prop="roleIds">
            <c-select v-model="form.roleIds" type="RoleName" multiple placeholder="请选择"></c-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取 消</el-button>
      <c-button type="primary" @click="btnOk">确 定</c-button>
    </div>
  </el-dialog>
</template>

<script>
import {mapState} from 'vuex'

export default {
  name: 'SDialogEdit',
  props: {
    data: {
      type: Object,
      default: function () {
        return {}
      }
    }
  },
  data () {
    return {
      loading: false,
      formLabelWidth: '120px',
      visible: true,
      form: {
        id: this.data.detail.id,
        icon: this.data.detail.icon,
        userName: this.data.detail.userName,
        realName: this.data.detail.realName,
        birthday: this.data.detail.birthday,
        mobile: this.data.detail.mobile,
        email: this.data.detail.email,
        qq: this.data.detail.qq,
        wx: this.data.detail.wx,
        departIds: '',
        roleIds: [],
        positionLevel: this.data.detail.positionLevel
      },
      rules: {
        userName: this.$checkRules.username,
        realName: this.$checkRules.name,
        mobile: [
          ...this.$checkRules.mobileR,
          ...this.$checkRules.mobile
        ],
        email: [
          ...this.$checkRules.email,
          ...this.$checkRules.emailR
        ],
        departIds: this.$checkRules.department,
        positionLevel: this.$checkRules.positionLevel,
        roleIds: this.$checkRules.roleOption
      }
    }
  },
  created () {
    this.$service.system.detailUser({id: this.data.detail.id}).then(res => {
      const data = res.data
      Object.assign(this.form, this.$lodash.pick(data.userInfo, ['id', 'icon', 'userName', 'realName', 'birthday', 'mobile', 'email', 'qq', 'wx', 'positionLevel']))
      if (this.$lodash.isEmpty(data.departments[0])) {
        this.form.departIds = ''
      } else {
        this.form.departIds = data.departments[0].id
      }
      const roleNames = []
      for (let {id} of data.roleNames) {
        roleNames.push(id)
      }
      this.form.roleIds = roleNames
    })
  },
  computed: {
    ...mapState('options', ['options'])
  },
  methods: {
    close () {
      this.$emit('close')
    },
    btnOk (btn) {
      this.$refs['form'].validate((valid) => {
        // 校验通过
        if (valid) {
          this.$service.system.updateUser(this.form).then(() => {
            this.$emit('ok')
            this.visible = false
          }).finally(() => {
            btn.isLoading(false)
          })
        } else {
          btn.isLoading(false)
        }
      })
    }
  }
}
</script>

<style>

</style>
